package com.tianji.promotion.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <p>
 * 线程池  配置类
 * </p>
 *
 * @author lcm
 * @since 2023-08-02
 */
@Configuration
public class PromotionConfig {

    @Bean
    public Executor generateExchangeCodeExecutor(){
        ThreadPoolTaskExecutor poolExecutor = new ThreadPoolTaskExecutor();

        //1.核心线程数大小
        poolExecutor.setCorePoolSize(3);
        //2.最大线程数
        poolExecutor.setMaxPoolSize(5);
        //3.队列大小
        poolExecutor.setQueueCapacity(200);
        //4.线程名称
        poolExecutor.setThreadNamePrefix("exchange-code-handler-");
        //5.拒绝策略
        poolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //6.线程池初始化
        poolExecutor.initialize();
        return poolExecutor;
    }

    @Bean
    public Executor discountSolutionExecutor(){
        ThreadPoolTaskExecutor poolExecutor = new ThreadPoolTaskExecutor();

        //1.核心线程数大小
        poolExecutor.setCorePoolSize(10);
        //2.最大线程数
        poolExecutor.setMaxPoolSize(9999);
        //3.队列大小
        poolExecutor.setQueueCapacity(200);
        //4.线程名称
        poolExecutor.setThreadNamePrefix("discount-handler-");
        //5.拒绝策略
        poolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //6.线程池初始化
        poolExecutor.initialize();
        return poolExecutor;
    }
}
